// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import LoadManager from '../resources/PPSDK/common/Manager/LoadManager';
import PoolUtil from '../resources/PPSDK/common/Utils/PoolUtil';
import {JSON, nodePool } from './common/miniGame_config'
import { miniGame_data } from "./common/miniGame_data";
import miniGame_pig from './miniGame_pig';

const {ccclass, property} = cc._decorator;

@ccclass
export default class   miniGame_game extends cc.Component {

    @property(cc.Node)
    pigItem:cc.Node=null;
    @property(cc.Label)
    level_level:cc.Label=null;
    @property(cc.Label)
    label_time:cc.Label=null;
    @property(cc.Label)
    label_tip:cc.Label=null;


    protected onLoad(): void {
        this.initPhysic();
        this.initData();
    }
    initPhysic(){
        cc.director.getPhysicsManager().enabled=true;
    }
    minLevel:number=0;
    maxLevel:number=0;
    levelJson=null;
    sumPig:number=0;
    countPig:number=0;
    timeCount:number=180;
    async initData(){
        await this.loadJson();
        this.createItem();
        this.showLevel();
        this.showTime();
        this.schedule(this.showTime,1)
    }
    
    async loadJson(){
        let url=JSON.level+miniGame_data.data.level;
        let Json:cc.JsonAsset=LoadManager.get(url,cc.JsonAsset)||await LoadManager.load(url,cc.JsonAsset);
        this.maxLevel=Json.json.length;
        this.levelJson=Json.json[this.minLevel];
    }
    async createItem() {
        //总共有多少只猪仔
        this.countPig=0;
        this.reCallArr=[];
        this.sumPig=this.levelJson["nodeArr"].length;
        for (let i = 0; i < this.levelJson["nodeArr"].length; i++) {
            //先从对象池里面获得
            let node:cc.Node=null;
            if (PoolUtil.size(nodePool.Pig) > 0) {
                node=PoolUtil.get(nodePool.Pig);
            } else {
                node= LoadManager.instantiate(this.pigItem);
            }
            this.node.addChild(node);
            node.scale=this.levelJson["scale"];
            node.position = cc.v3(this.levelJson["nodeArr"][i].x, this.levelJson["nodeArr"][i].y);
            node.angle = this.levelJson["nodeArr"][i].angle;
            node.getComponent(miniGame_pig).game=this;
            node.getComponent(miniGame_pig).initData();
        }
    }
    success(){
        this.countPig++;
        this.reCallArr=[];
        if(this.countPig==this.sumPig){
            //第一小关是否成功了
            if(this.minLevel<this.maxLevel){
                this.minLevel++;
                this.initData();
            }
            else{
                this.minLevel=0;
                console.log("进入结算界面~~~~");//
            }
        }
    }
    /**UI相关 */
    showLevel(){
        this.level_level.string="关卡:"+(this.minLevel+1)+"/"+(this.maxLevel);
    }
    showTime(){
        this.label_time.string=this.timeCount+'';
        this.timeCount--;
        if(this.timeCount==0){
            this.unschedule(this.showTime);
            //出现UI
        }
    }
    showTip(str:string){
        this.label_tip.node.active=true;
        cc.Tween.stopAllByTarget(this.label_tip.node);
        this.label_tip.node.position=cc.v3(0,-300,0)
        this.label_tip.string=str;
        cc.tween(this.label_tip.node).by(1,{y:300},cc.easeBackOut()).call(()=>{this.label_tip.node.active=false}).start();
    }
    isClick:boolean=false;
    reCallArr=[];
    onClickBtn(event:cc.Event,customData:string){
        if(this.isClick){return}
        this.scheduleOnce(()=>{this.isClick=false},0.5)
        switch(customData){
            case 'recall':
                if(this.reCallArr.length<=0){
                    this.showTip("不可撤回");
                }else{
                    let obj=this.reCallArr.pop();
                    obj.node.position=obj.pos;
                }
                break;
            case 'addTime':
                //
                this.showTip("成功加时");
                this.timeCount+=60;
                break;
            case 'vanish':
                if(!miniGame_data.isVanish){
                    miniGame_data.isVanish=true;
                }
                this.showTip("请将某只动物抓走");
                break;
            case 'turn':
                //可以选择某一个动物进行翻转
                if(!miniGame_data.isTurn){
                    miniGame_data.isTurn=true;
                }
                this.showTip("请将某只动物翻转");
                break;
        }

    }
}
